<?php

class administration_View extends View {
     
    public function createView($action, $params) {
        switch($action) {
            
            /* ----- MAIN MENUS ------ */
            
            
            //ADMINISTRATION MAIN MENU
            case 'menu': 
                $buttons = array();
                $buttons[] = array('action'=>'accounts', 'text'=>'Zarządzaj grupami lub kontami użytkowników');
                $buttons[] = array('action'=>'rights', 'text'=>'Zarządzaj prawami użytkowników i grup');
                
                $this->view = Menu::createMenu($buttons, $params, true);
                
            break;
        
            //ADMINISTRATION ACCOUNTS MENU
            case 'accounts':
                $buttons = array();
                $buttons[] = array('action'=>'createUser','text'=>'Dodaj konto');
                $buttons[] = array('action'=>'addGroup','text'=>'Dodaj grupę');
                $buttons[] = array('action'=>'_default','text'=>'Powrót');
                $buttons[] = array('action'=>'delUser','text'=>'Usuń konto');
                $buttons[] = array('action'=>'delGroup','text'=>'Usuń grupę');
                $buttons[] = array('action'=>'editUser','text'=>'Edytuj konto');
                $buttons[] = array('action'=>'editGroup','text'=>'Edytuj grupę');
                
                $this->view = Menu::createMenu($buttons, $params, false,array(3,2,2));
            break;
        
        
            //ADMINISTRATION RIGHTS MENU
            case 'rights':
                $buttons = array();
                $buttons[] = array('action'=>'userRights','text'=>'Prawa użytkowników');
                $buttons[] = array('action'=>'groupRights','text'=>'Prawa grup');
                $buttons[] = array('action'=>'_default','text'=>'Powrót');
                $this->view = Menu::createMenu($buttons, $params, false);
            break;
        
   
            /*  ----- USER ----- */
        
        
            //CREATE USER
            case 'createUserError':
                $this->view = $this->errorInfo("Nie udało się stworzyć konta użytkownika.");
            case 'createUser':
                if(isset($params['userInfo'])) {
                     $userInfo = $params['userInfo'];
                     unset($params['userInfo']);
                } else {
                    $userInfo = array();
                }
                $this->view .= $this->userForm($this->createLink($params),$userInfo,"Utwórz konto");
            break;
            case 'createUserSuccess':
                $buttons = array();
                $buttons[] = array('action'=>'accounts','text'=>'Powrót');
                $user = new AnyUser((int)$params['uid']);
                unset($params['uid']);
                $this->view = 'Użytkownik ' . $user->getName() . ' ' . $user->getSurname() . ' (' . $user->getLogin() . ') został utworzony (id:' . $user->getUid() . ')';
                $this->view .= Menu::createMenu($buttons, $params);
            break;
            
        
            //DELETE USER
            case 'deleteUserError': 
                $this->view .= '<span class="error">Nie udało się usunąć użytkownika ' . $params['details'] . ' id:' . $params['uid'] . '</span>';
                unset($params['details']);
                unset($params['uid']);
            case 'deleteUser':
                $f = new Form($this->createLink($params), 'post');
                    $f->addSection("Wybór użytkownika",'delUserForm');
                        $f->section()->add(new PureHtml('<span>Wpisz przynajmniej 3 znaki imienia, nazwiska lub loginu użytkownika</span>'));
                        $f->section()->add(new Input('','text','user','','','','','user'));
                        $f->section()->add(new Input('','hidden','uid','0','','','','uid'));
                        $f->section()->add(new Input('','hidden','details','','','','','details'));
                    $f->addButton(new Input('','submit','sbmt','Usuń','','','onClick="return confirm(\'Czy na pewno chcesz usunąć konto tego użytkownika? Wiąże się to z usunięciem wszystkich danych o użytkowniku z systemu!\');"','delete'));
                    $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'index.php?module=' . $params['module'] . '&action=accounts\'"'));
                $this->view .= $f->get();
            break;
            case 'deleteUserSuccess':
                $this->view .= 'Użytkownik ' . $params['details'] . ' id:' . $params['uid'] . ' został usunięty z systemu.';
                unset($params['details']);
                unset($params['uid']);
                $buttons = array();
                $buttons[] = array('action'=>'accounts','text'=>'Powrót');
                $this->view .= Menu::createMenu($buttons, $params);
            break;
        
        
            //EDIT USER
            case 'editUserError':
                $this->view = $this->errorInfo("Nie udało się dokonać zmian na koncie użytkownika.");
            case 'editUser':
                $f = new Form($this->createLink($params), 'post');
                    $f->addSection("Wybór użytkownika",'delUserForm');
                        $f->section()->add(new PureHtml('<span>Wpisz przynajmniej 3 znaki imienia, nazwiska lub loginu użytkownika</span>'));
                        $f->section()->add(new Input('','text','user',isset($_POST['details']) ? $_POST['details'] : '' ,'','',isset($_POST['details']) ? 'disabled="disabled"' : '','user'));
                        $f->section()->add(new Input('','hidden','uid',isset($_POST['uid']) ? $_POST['uid'] : '0','','','','uid'));
                        $f->section()->add(new Input('','hidden','details',isset($_POST['details']) ? $_POST['details'] : '','','','','details'));
                    $f->addButton(new Input('','submit','sbmt','Edytuj','','',isset($_POST['uid']) ? 'disabled="disabled"' : '','edit'));
                    $f->addButton(new Input('','button','other','Inny użytkownik','','',isset($_POST['uid']) ? '' : 'disabled="disabled"','other'));
                    $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'index.php?module=' . $params['module'] . '&action=accounts\'"'));
                $this->view .= $f->get();
                if(isset($_POST['uid'])) {
                    $user = new AnyUser((int)$_POST['uid']);
                    $this->view .= $this->userForm($this->createLink($params),$user,"Zatwierdź zmiany");
                }
            break;
            case 'editUserSuccess':
                $buttons = array();
                $buttons[] = array('action'=>'accounts','text'=>'Powrót');
                $user = new AnyUser((int)$params['uid']);
                unset($params['uid']);
                $this->view = 'Zmiany zostały wprowadzone [' . $user->getName() . ' ' . $user->getSurname() . ' (' . $user->getLogin() . ', id:' . $user->getUid() . ')]';
                $this->view .= Menu::createMenu($buttons, $params);
            break;
        
   
            /*  ----- GROUP ----- */
        
        
            //ADD GROUP
            case 'addGroupError':
                $this->view = $this->errorInfo("Nie udało się dodać grupy.");
            case 'addGroup':
                $f = new Form($this->createLink($params), "post","groupForm");
                    $f->addSection("Dane grupy", "groupForm");
                        $f->section()->add(new Input("Nazwa: ", "text", "name", isset($_POST['name']) ? $_POST['name'] : '', "", "", "", "groupName"));
                    $f->addButton(new Input("", "submit", "sbmt", "Dodaj grupę", "", "", "", "addGroup"));
                $this->view .= $f->get();
            break;
            case 'addGroupSuccess':
                $buttons = array();
                $buttons[] = array('action'=>'accounts','text'=>'Powrót');
                $group = new Group((int)$params['gid']);
                unset($params['gid']);
                $this->view = 'Grupa <b>' . $group->name . '</b> została dodana (id:' . $group->getGid() . ')';
                $this->view .= Menu::createMenu($buttons, $params);
            break;
        
            //DELETE GROUP
            case 'delGroupError':
                $this->view = $this->errorInfo("Nie udało się usunąć grupy.");
            case 'delGroup':
                $f = new Form($this->createLink($params), "post", "groupForm");
                    $f->addSection("Wybierz grupę");
                        $f->section()->add(new Select("", GroupDAO::getGroups(true), "gid", 0));
                    $f->addButton(new Input('', 'submit', 'sbmt', 'Usuń','','','onClick="return confirm(\'Czy na pewno chcesz usunąć wybraną grupę z systemu?\');"'));
                $this->view .= $f->get();
            break;
            case 'delGroupSuccess':
                $buttons = array();
                $buttons[] = array('action'=>'accounts','text'=>'Powrót');
                
                $this->view = 'Grupa <b>' . $params['groupName'] . '</b> została usunięta. <br />';
                if(count($params['users'])) {
                    $this->view .= 'Następujący użytkownicy nie posiadają przypisanej żadnej grupy (' . count($params['users']) . '):<ul>';
                    foreach($params['users'] as $key => $user) {
                        $this->view .= "<li>" . $user->getName() . ' ' . $user->getSurname() . ' (' . $user->getLogin() . ', id:' . $user->getUid() . ')</li>';
                    }
                    $this->view .= '</ul>';
                }
                unset($params['groupName']);
                unset($params['users']);
                $this->view .= Menu::createMenu($buttons, $params);
            break;

            
            //EDIT GROUP
            case 'editGroupError':
                $this->view = $this->errorInfo("Nie udało się zaktualizować danych grupy.");
            case 'editGroup':
                $f = new Form($this->createLink($params), "post", "groupForm");
                    $f->addSection("Wybierz grupę");
                        $f->section()->add(
                                            new Select( "", 
                                                        GroupDAO::getGroups(true),
                                                        "gid", isset($_POST['gid']) ? (int)$_POST['gid'] : '0' ,
                                                        '',
                                                        '',
                                                        'onChange="$(\'.groupForm\').submit()"'
                                            ));
                        if(isset($_POST['gid'])) {
                            $group = new Group((int)$_POST['gid']);
                            $f->section()->add(new Input('Nazwa: ', 'text', 'name', $group->name));
                            $f->addButton(new Input('', 'submit', 'sbmt', 'Zatwierdź zmiany'));
                        }
                $this->view .= $f->get();
            break;
            case 'editGroupSuccess':
                $buttons = array();
                $buttons[] = array('action'=>'accounts','text'=>'Powrót');
                
                $this->view = 'Informacje o grupie zostały pomyślnie zmodyfikowane.';
                $this->view .= Menu::createMenu($buttons, $params);
            break;
        
   
            /*  ----- USER RIGHTS ----- */
        
        
            case 'userRights':
                $f = new Form($this->createLink($params), 'post');
                    $f->addSection("Wybór użytkownika",'delUserForm');
                        $f->section()->add(new PureHtml('<span>Wpisz przynajmniej 3 znaki imienia, nazwiska lub loginu użytkownika</span>'));
                        $f->section()->add(new Input('','text','user','' ,'','','','userRights'));
                    $f->addButton(new Input('','button','other','Inny użytkownik','','','disabled="disabled"','other'));
                    $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'index.php?module=' . $params['module'] . '&action=rights\'"'));
                $this->view .= $f->get();
                $this->view .= '<div id="userRightsInfo" style="display: none;"></div>';
                $this->view .= '<div id="actions" style="display: none;"></div>';
            break;
        
   
            /*  ----- GROUP RIGHTS ----- */
        
        
            case 'groupRights':
                $f = new Form($this->createLink($params), 'post');
                    $f->addSection("Wybór grupy",'delUserForm');
                        $f->section()->add(new PureHtml('<span>Zacznij pisać nazwę grupy użytkowników</span>'));
                        $f->section()->add(new Input('','text','user','' ,'','','','groupRights'));
                    $f->addButton(new Input('','button','other','Inna grupa','','','disabled="disabled"','otherGroup'));
                    $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'index.php?module=' . $params['module'] . '&action=rights\'"'));
                $this->view .= $f->get();
                $this->view .= '<div id="groupRightsInfo" style="display: none;"></div>';
                $this->view .= '<div id="actions" style="display: none;"></div>';
            break;
        }
    }
    
    private function userForm($action,$userInfo = array(),$buttonText) {
        if(is_object($userInfo)) {
            $u = $userInfo;
            $userInfo = array();
            $userInfo['login'] = $u->getLogin();
            $userInfo['password'] = '';
            $userInfo['password2'] = '';
            $userInfo['name'] = $u->getName();
            $userInfo['surname'] = $u->getSurname();
            $userInfo['gid'] = $u->getGid();
            $userInfo['enabled'] = $u->getEnabled();
            $userInfo['uid'] = $u->getUid();
        }
        $f = new Form($action,'post');
            $f->addSection('Dane użytkownika','userForm');
                $f->section()->add(new Input('Login: ','text','login',isset($userInfo['login']) ? $userInfo['login'] : '','','','','login'));
                $f->section()->add(new Input('Hasło: ','password','password',isset($userInfo['password']) ? $userInfo['password'] : '','','','autocomplete="off"','pass'));
                $f->section()->add(new Input('Powtórz hasło: ','password','password2',isset($userInfo['password2']) ? $userInfo['password2'] : '','','','autocomplete="off"','pass2'));
                $f->section()->add(new Input('Imię: ','text','name',isset($userInfo['name']) ? $userInfo['name'] : '','','','','name'));
                $f->section()->add(new Input('Nazwisko: ','text','surname',isset($userInfo['surname']) ? $userInfo['surname'] : '','','','','surname'));
                $groups = GroupDAO::getGroups();
                $select = array();
                $select[] = array('id'=>0,'text'=>'---Wybierz grupę---');
                foreach($groups as $key => $object) {
                    $select[] = array('id'=> $object->gid, 'text'=> $object->name);
                }
                $f->section()->add(new Select('Grupa: ',$select,'gid',isset($userInfo['gid']) ? $userInfo['gid'] : 0,'','','','gid'));
                $f->section()->add(new Checkbox('Włączony','enabled',2,(isset($userInfo['enabled']) && $userInfo['enabled'] == 1) ? true : false,1,'','','','enabled'));
                if(isset($userInfo['uid'])) {
                    $f->section()->add(new Input('','hidden','uid',$userInfo['uid']));
                }
                $f->section()->add(new Input('','hidden','commit',1));
            $f->addButton(new Input('','submit','sbmt',$buttonText));
            $action = substr($action,0,stripos($action,'action')-1);
            $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'' . $action . '&action=accounts\'"'));
        return $f->get();
    }
}

?>
